/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cl.darksigns.webstat.client;

import cl.darksigns.webstat.back.ejb.GetClientMessageRemote;
import cl.darksigns.webstat.back.exception.WebStatException;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import org.apache.log4j.Logger;

/**
 *
 * @author _ViKtOr
 */
public class WebStatDelegate {
    
    Logger logger = Logger.getLogger(WebStatDelegate.class);
    GetClientMessageRemote remote ;
    ResourceBundle res = ResourceBundle.getBundle("wst_client");
    
    public WebStatDelegate(){
        this.remote = this.lookupGetClientMessageBean();
    }
    private GetClientMessageRemote lookupGetClientMessageBean() {
        try {
            /*Properties props = new Properties();
            props.put(javax.naming.Context.PROVIDER_URL, res.getString("RMI_PROVIDER_URL"));
            props.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, res.getString("RMI_NAME_SERVICE"));
            Context c = new InitialContext(props);
            NamingEnumeration list = c.list("");
            while (list.hasMore()) {
                NameClassPair ncPair = (NameClassPair) list.next();
                logger.debug(ncPair.getName() + " (type " + ncPair.getClassName() + ")");
            }*/
            Context c = new InitialContext();
            return (GetClientMessageRemote) c.lookup(res.getString("RMI_JNDI"));
        } catch (NamingException ne) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    public String receiveMessage(String xml){
       return  remote.receiveMessage(xml);
    }
    public String saveStat(String xml) throws WebStatException {
        return remote.receiveSpecificMessage(xml);
    }

}
